package top.kent90.controller;



import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.kent90.common.object.AjaxDone;
import top.kent90.common.object.AtreeNode;
import top.kent90.common.object.Result;
import top.kent90.pojo.ZczyAtegory;
import top.kent90.service.ZczyAtegoryService;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("Ategory")
public class ZczyAtegoryController {
    @Reference
    ZczyAtegoryService service;
    /**
     * 到项目分类页面
     * @return
     */
    @RequestMapping("toAtegory")
    public String query(){
        return "ategory/ategory";
    }
    @RequestMapping("toAtegoryAdd")
    public String totoAtegoryAdd(){
        return "ategory/ategoryAdd";
    }
    @RequestMapping("toAtegoryUpd")
    public String toAtegoryUpd(){return "ategory/ategoryUpd";}
    /**
     * 测试页面
     * */
    @RequestMapping("reserve")
    public String test(){
        return "reserve";
    }
    /**
     * 项目分类列表
     * */


    /**
     * 树形数据
     *  private int id;
     *     private String name;
     *     private String brief;
     *     private String createTime;
     *     private String createBy;
     *     private List<AtreeNode> children ;
     * */
    @RequestMapping("tree")
    @ResponseBody
    public Result getAtree(){
        //查询所有父节点
        List<ZczyAtegory> listAll = service.queryByParentId(0);
        //创建树
        List<AtreeNode> atree = nodeList(listAll);
        return new Result("0","",100L,atree);
    }

    /**
     * 项目分类树形下拉框
     * @return
     */
    @RequestMapping("treeSelect")
    @ResponseBody
    public List treeSelect(){
        Result rs = getAtree();
        List<AtreeNode> list = (List<AtreeNode>)rs.getData();
        return list;
    }

    public List<AtreeNode> nodeList (List<ZczyAtegory> zalist){
        List<AtreeNode> AtreeList = new ArrayList<>();
        //遍历父节点 并将节点注入树中 作为一级菜单
        for(ZczyAtegory at :zalist){

            AtreeNode atreeNode = new AtreeNode();
            atreeNode.setId(at.getCategoryId());
            atreeNode.setTitle(at.getName());
            atreeNode.setName(at.getName());
            atreeNode.setBrief(at.getBrief());
            atreeNode.setCreateBy(at.getCreateBy());
            atreeNode.setCreateTime(at.getCreateTime());
            //判断是否有子节点
            if(at.getChildNum()>0){
                Integer id = at.getCategoryId();
                //根据父节点id查询其子节点
                List<ZczyAtegory> children = service.queryByParentId(id);
                //递归 将子节点 加入树中
                atreeNode.setChildren(nodeList(children));
            }
                //将节点注入树中
            AtreeList.add(atreeNode);
            }

        return AtreeList;
    }
    /**
     * 增加分类
     * */
    @RequestMapping("addAtegory")
    @ResponseBody
    public AjaxDone addAtegory(HttpServletRequest request){
        String pid = request.getParameter("id");
        String name = request.getParameter("name");
        String brief = request.getParameter("brief");
       int rs = service.addAtegory(pid,name,brief);
        if(rs>0){
        return new AjaxDone("200","请求成功");
        }else {
            return new AjaxDone("300","请求失败");
        }
    }
    /**
     * 删除项目分类
     * */
    @RequestMapping("delAtegory")
    @ResponseBody
    public AjaxDone delAtegory(String id){
       int rs =  service.delAtegory(id);
        if(rs>0){
            return new AjaxDone("200","请求成功");
        }else {
            return new AjaxDone("300","请求失败");
        }
    }
    /**
     *
     * @return
     *
     */
    @RequestMapping("ategoryUpd")
    @ResponseBody
    public AjaxDone toUpdGoodsClass(HttpServletRequest request) {
        String categoryId = request.getParameter("id");
        String name = request.getParameter("name");
        String brief = request.getParameter("brief");
        int rs = service.updAtegory(categoryId,name,brief);
        if(rs>0){
            return new AjaxDone("200","请求成功");
        }else {
            return new AjaxDone("300","请求失败");
        }

    }
}
